package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.JyjcSharingExpensesDataDemo;
import com.ruoyi.system.mapper.ExpensesDemoMapper;
import com.ruoyi.system.domain.ExpensesDemo;
import com.ruoyi.system.service.IExpensesDemoService;

/**
 * 费用分摊模板管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-04
 */
@Service
public class ExpensesDemoServiceImpl implements IExpensesDemoService
{
    @Autowired
    private ExpensesDemoMapper expensesDemoMapper;

    /**
     * 查询费用分摊模板管理
     *
     * @param id 费用分摊模板管理主键
     * @return 费用分摊模板管理
     */
    @Override
    public ExpensesDemo selectExpensesDemoById(Long id)
    {
        return expensesDemoMapper.selectExpensesDemoById(id);
    }

    /**
     * 查询费用分摊模板管理列表
     *
     * @param expensesDemo 费用分摊模板管理
     * @return 费用分摊模板管理
     */
    @Override
    public List<ExpensesDemo> selectExpensesDemoList(ExpensesDemo expensesDemo)
    {
        return expensesDemoMapper.selectExpensesDemoList(expensesDemo);
    }

    /**
     * 新增费用分摊模板管理
     *
     * @param expensesDemo 费用分摊模板管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertExpensesDemo(ExpensesDemo expensesDemo)
    {
        int rows = expensesDemoMapper.insertExpensesDemo(expensesDemo);
        insertJyjcSharingExpensesDataDemo(expensesDemo);
        return rows;
    }

    /**
     * 修改费用分摊模板管理
     *
     * @param expensesDemo 费用分摊模板管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateExpensesDemo(ExpensesDemo expensesDemo)
    {
        expensesDemoMapper.deleteJyjcSharingExpensesDataDemoBySeId(expensesDemo.getId());
        insertJyjcSharingExpensesDataDemo(expensesDemo);
        return expensesDemoMapper.updateExpensesDemo(expensesDemo);
    }

    /**
     * 批量删除费用分摊模板管理
     *
     * @param ids 需要删除的费用分摊模板管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExpensesDemoByIds(Long[] ids)
    {
        expensesDemoMapper.deleteJyjcSharingExpensesDataDemoBySeIds(ids);
        return expensesDemoMapper.deleteExpensesDemoByIds(ids);
    }

    /**
     * 删除费用分摊模板管理信息
     *
     * @param id 费用分摊模板管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteExpensesDemoById(Long id)
    {
        expensesDemoMapper.deleteJyjcSharingExpensesDataDemoBySeId(id);
        return expensesDemoMapper.deleteExpensesDemoById(id);
    }

    /**
     * 新增分摊费用子信息
     *
     * @param expensesDemo 费用分摊模板管理对象
     */
    public void insertJyjcSharingExpensesDataDemo(ExpensesDemo expensesDemo)
    {
        List<JyjcSharingExpensesDataDemo> jyjcSharingExpensesDataDemoList = expensesDemo.getJyjcSharingExpensesDataDemoList();
        Long id = expensesDemo.getId();
        if (StringUtils.isNotNull(jyjcSharingExpensesDataDemoList))
        {
            List<JyjcSharingExpensesDataDemo> list = new ArrayList<JyjcSharingExpensesDataDemo>();
            for (JyjcSharingExpensesDataDemo jyjcSharingExpensesDataDemo : jyjcSharingExpensesDataDemoList)
            {
                jyjcSharingExpensesDataDemo.setSeId(id);
                list.add(jyjcSharingExpensesDataDemo);
            }
            if (list.size() > 0)
            {
                expensesDemoMapper.batchJyjcSharingExpensesDataDemo(list);
            }
        }
    }
}
